<?php

use common\widgets\ActiveForm;
use common\widgets\Form;
use common\widgets\pages\CustomTabs as Tabs;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $form ActiveForm */
/* @var $this yii\web\View */
/* @var $formModel common\components\ActiveRecord */
/* @var $dataProvider yii\data\ActiveDataProvider */
/** @var string $method */

$model = $formModel;
$this->title = isset($pageTitle) ? $pageTitle :
    method_exists($model,"classLabelName") ? $model->classLabelName() :  $model->className();

$_o = new ReflectionClass( $model );
$methodLabelMap = [$_o->getconstant("SCENARIO_CREATE") =>"新增", $_o->getconstant("SCENARIO_UPDATE") =>"更新"];
$this->params['subtitle'] = $methodLabelMap[$method];
$this->params['tabId'] = md5(microtime(true));
$this->params['breadcrumbs'] = [
    $this->title
];
$this->params['breadcrumbs'][] = $this->title . ' ' . $this->params['subtitle'];

$formContent = '';
$form = ActiveForm::begin(
    [
        'type' => ActiveForm::TYPE_VERTICAL,
        'options' => ['enctype' => 'multipart/form-data'],
        //'action' => isset($method) ? \yii\helpers\Url::to($method) : \yii\helpers\url::current()
    ]);

$extra = isset($extra) ? $extra : null;
if (method_exists($formModel, "formRows") && $formModel->formRows($form, $model, $extra)) {
    foreach ($formModel->formRows($form, $model, $extra) as $formRow) {

        $defaultRowConfig = [
            'model' => $model,
            'form' => $form,
        ];
        $formRow = ArrayHelper::merge($defaultRowConfig,$formRow);
        if (!isset($formRow['columns'])) {
            $formRow['columns'] = count($formRow['attributes']) > 0 ? count($formRow['attributes'])  : 1;
        }

        //特定场景不渲染
        if(isset($formRow['on']) && is_array($formRow['on']) ){
            if(!in_array($model->scenario,$formRow['on'])) {
                continue;
            }
        }

        $formWidget = Form::widget($formRow);
        if( isset($formRow['box']) ){
            $formRow['box']['title'] = isset($formRow['box']['title']) ? $formRow['box']['title'] : "";
            $widgetContent = <<<EOF
        <div class="box box-success">
            <div class="box-header with-border">
                <h3 class="box-title" style="font-size:16px">{$formRow['box']['title']}</h3>
                <div class="box-tools pull-right">
                    <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                </div><!-- /.box-tools -->
            </div><!-- /.box-header -->
            <div class="box-body">
                {$formWidget}
            </div><!-- /.box-body -->
        </div>
EOF;
        }else{
            $widgetContent = $formWidget;
        }

        $formContent .= $widgetContent;

    }
}

$tabContent = $formContent;
$tabsConfig['title'] = $this->title;
$tabsConfig['id'] = $this->params['tabId'];
$tabsConfig["items"][] = [
    "label" => $this->params['subtitle'],
    "content" => $tabContent . Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']),
];

$content = Tabs::widget($tabsConfig);


echo Html::tag("div",
    Html::tag("div", $content, ['class' => "col-xs-12 " . \yii\helpers\Inflector::slug($model->className()) . "grid-view"])
    , ['class' => "row"]);